package com.androidtraining.lab5;

import com.example.acamp.dip.R;

import android.app.Activity;
import android.app.ListFragment;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class PicListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {

    // This is the Adapter being used to provision the list's data.
    CursorAdapter mAdapter;

    private Callbacks mCallbacks;
    
    public interface Callbacks {
        public void onItemSelected(String urlString, String itemName);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        mAdapter = new CursorAdapter(getActivity(), null, 0) {

			@Override
			public void bindView(View v, Context ctx, Cursor cur) {
				TextView tv = (TextView)v.findViewById(android.R.id.text1);
				String urlString = cur.getString(1);
				Uri uri = Uri.parse(urlString);
				tv.setText(Uri.decode(Uri.decode(uri.getLastPathSegment())));	// column index is from PROJ_IMAGE_URL
				tv.setTag(urlString);
			}

			@Override
			public View newView(Context ctx, Cursor cur, ViewGroup vg) {

				LayoutInflater li = getActivity().getLayoutInflater();
				View v = li.inflate(R.layout.simple_list_item, vg, false);
				return v;
			}
        	
        };
        setListAdapter(mAdapter);
        // Prepare the loader. Either re-connect with an existing one,
        // or start a new one.
        getLoaderManager().initLoader(0, null, this);
    }
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (!(activity instanceof Callbacks)) {
            throw new IllegalStateException("Activity must implement fragment's callbacks.");
        }

        mCallbacks = (Callbacks) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);
		TextView tv = (TextView)view.findViewById(android.R.id.text1);        
        mCallbacks.onItemSelected((String)tv.getTag(),tv.getText().toString());
    }

    // These are the rows that we will retrieve.
    public static final String[] PROJ_IMAGE_URL = new String[] { 
            PicasaProvider.PicasaFeaturedMetadataDB._ID,
            PicasaProvider.PicasaFeaturedMetadataDB.IMAGE_URL, 
    };    
    
    @Override
    public Loader<Cursor> onCreateLoader(int loaderId, Bundle bundle) {
        CursorLoader cl = new CursorLoader(getActivity());
        cl.setUri(PicasaProvider.getUriByType(getActivity(), PicasaProvider.METADATA_QUERY));
        cl.setProjection(PROJ_IMAGE_URL);
        return cl;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        mAdapter.swapCursor(cursor);
        setListShown(true);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);
    }
}
